/**
 * 
 */
package bedside.gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

import bedside.VitalSign;

import common.VitalSignListener;
import common.VitalSignUpdate;
import common.gui.VitalSignDisplay;

/**
 * @author Eric Lutley
 * 
 */
public class BedsideMonitorDisplay extends JPanel implements ActionListener,
		VitalSignListener {

	private static final String CALL_BUTTON = "Call Button";
	private static final int VITAL_COLUMNS = 1;

	private Map<VitalSign, VitalSignDisplay> vitalSignMap;

	public BedsideMonitorDisplay(Collection<VitalSign> vitalSigns) {

		vitalSignMap = new HashMap<VitalSign, VitalSignDisplay>();

		Container vitalPanel = new JPanel();
		vitalPanel.setLayout(new GridLayout(0, VITAL_COLUMNS));

		for (VitalSign vs : vitalSigns) {
			VitalSignDisplay vsd = new VitalSignDisplay(vs.getType(),
					Color.BLACK);
			vsd.addActionListener(this);
			vsd.setBorder(BorderFactory.createLineBorder(Color.BLACK));

			vitalSignMap.put(vs, vsd);

			vitalPanel.add(vsd);
		}
		JToggleButton callButton = new JToggleButton(CALL_BUTTON);
		callButton.setActionCommand(CALL_BUTTON);
		callButton.addActionListener(this);
		this.add(callButton);
		this.add(vitalPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Edit the vital sign associated with the display that raised the event
		String action = e.getActionCommand();
		if ( action.equals( VitalSignDisplay.ACTION_SET_ALARM ) ) {
			VitalSignDisplay source = (VitalSignDisplay) e.getSource();
			for ( VitalSign vs : vitalSignMap.keySet() ) {
				if ( vitalSignMap.get( vs ) == source  ) {
					SetAlarmDialog dialog = new SetAlarmDialog( vs );
					dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
					dialog.setVisible( true );
				}
				// Now update the vital sign display
				// TODO: Add ability to get limits from vital sign class
				//source.setAlarmLimits(critLow, low, high, critHigh);
			}
		}
	}

	public static void main(String[] args) {
		JFrame testFrame = new JFrame("Bedside Monitor Test");

		List<VitalSign> vitals = new ArrayList<VitalSign>();
		VitalSign vital = new VitalSign(25, 50, 110, 160, 10000,
				VitalSign.HEART);
		vitals.add(vital);
		vital = new VitalSign(6, 8, 24, 30, 10000, VitalSign.LUNGS);
		vitals.add(vital);

		BedsideMonitorDisplay display = new BedsideMonitorDisplay(vitals);
		testFrame.setContentPane(display);
		testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		testFrame.pack();
		testFrame.setVisible(true);
	}

	@Override
	public void updateVitalSign(VitalSignUpdate reading) throws RemoteException {
		VitalSign type = reading.type;
		VitalSignDisplay vsd = vitalSignMap.get(type);
		vsd.setReading(reading.reading);
	}

	@Override
	public void updateVitalSigns(Collection<VitalSignUpdate> readings)
			throws RemoteException {
		final Collection<VitalSignUpdate> readingList = readings;
		Runnable update = new Runnable() {
			public void run() {
				try {
					for (VitalSignUpdate vsu : readingList) {
						updateVitalSign(vsu);
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		};
		try {
			SwingUtilities.invokeAndWait( update );
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
